﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Edu;
using ZHDJ.WebPage;

namespace SysManage.Module.Edu
{
    public partial class PatyEduSubjectEdit : BLL.PageBase
    {
        public string id { get { return GetQueryIntValue("id"); } }
        public string specialID { get { return GetQueryIntValue("SpecialID"); } }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return string.IsNullOrEmpty(id) ? "PartyEduSpecialAdd" : "PartyEduSpecialEdit";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();

            
                IQueryable<PartyEduSpecial> q = DB.PartyEduSpecials;
                //this.ddlSpecial.DataSource = q;
                //this.ddlSpecial.DataBind();

            ControlBusiness.ControlBind(ddlSpecial, q, "SpecialName", "ID");
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyEduSubject current = DB.PartyEduSubjects.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                this.ddlSpecial.SelectedValue = current.SpecialID;
                tbxSubjectcontent.Text = current.Subjectcontent;
                cbxEnabled.Checked = current.Enabled;
                ddlSpecial.Readonly = true;

                var op = current.PatyEduSubjectOptions.OrderBy(a => a.OrderNo);
                var index = 1;
                foreach (var item in op)
                {
                    var text = FineUI.ControlUtil.FindControl("TextBox" + index) as FineUI.TextBox;
                    var radio = FineUI.ControlUtil.FindControl("RadioButtonList" + index) as FineUI.RadioButtonList;
                    if (text == null || radio == null)
                    {
                        continue;
                    }
                    text.Text = item.OptionsContent;
                    radio.SelectedValue = item.IsAnswer ? "1" : "0";
                    index += 1;
                }
            }
            else
            {
                this.ddlSpecial.SelectedValue = specialID;
            }
        }

        #endregion

        #region Events

        private void SaveJobTitle()
        {
            //删除选项
            var subjectid = "";
            string id = GetQueryIntValue("id");
            subjectid = id;
            if (id != "")
            {
                //更新
                PartyEduSubject item = DB.PartyEduSubjects.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                item.SpecialID = this.ddlSpecial.SelectedValue;
                item.Subjectcontent = tbxSubjectcontent.Text.Trim();
                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.ID;
                item.Enabled = cbxEnabled.Checked;
                //移除选项
                DB.PartyEduSubjectOptions.Where(u => u.SubjectID==item.ID).ToList().ForEach(u => DB.PartyEduSubjectOptions.Remove(u));
            }
            else
            {
                PartyEduSubject item = new PartyEduSubject();
                subjectid=item.ID = StringHelper.NewID;
                item.SpecialID = this.ddlSpecial.SelectedValue;
                item.Subjectcontent = tbxSubjectcontent.Text.Trim();
                item.Enabled = cbxEnabled.Checked;
                item.CreateTime = DateTime.Now;
                item.CreateBy = CurrentUser.ID;
                item.SubjectType = "1";
                item.Answer = "1";
                DB.PartyEduSubjects.Add(item);
                
            }
            DB.SaveChanges();
            //保存选项
            for (int i = 1; i < 9; i++)
            {
                var text = FineUI.ControlUtil.FindControl("TextBox" + i) as FineUI.TextBox;
                var radio = FineUI.ControlUtil.FindControl("RadioButtonList" + i) as FineUI.RadioButtonList;
                if (text == null || radio == null)
                {
                    continue;
                }
                if (radio.SelectedValue == "" || text.Text.Trim() == "")
                {
                    continue;
                }
                PartyEduSubjectOptions op = new PartyEduSubjectOptions();
                op.ID = StringHelper.NewID;
                op.IsAnswer = radio.SelectedValue == "1";
                op.OptionsContent = text.Text;
                op.OrderNo = i;
                op.SubjectID = subjectid;
                DB.PartyEduSubjectOptions.Add(op);
            }
            DB.SaveChanges();
        }
       


        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveJobTitle();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

    }
}
